package com.tencent.wxcloudrun.init;

import com.alibaba.fastjson.JSONObject;
import com.tencent.wxcloudrun.vo.FinalClass;
import com.tencent.wxcloudrun.vo.GetAPIInfo;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
//计时器，项目启动后会自动运行
@Component
public class SetTimerToGetAccessToken {
    private static final SimpleDateFormat DATA = new SimpleDateFormat("HH:mm:ss");
//每7150秒运行一次，用来获取新的accessToken
    @Scheduled(fixedRate = 7150000)
    public void testTasks(){
        String appId = FinalClass.getAppId();
        String appSecret = FinalClass.getAppSecret();
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+appSecret;

        GetAPIInfo getAPIInfo = new GetAPIInfo();
        JSONObject jsonObject = getAPIInfo.getAPIInfo(url);
        AccessToken.accessToken = jsonObject.getString("access_token");
        System.out.println(AccessToken.accessToken);
    }
}
